package Demo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2022-11-27
 * Time: 15:52
 */

class Increast {
    private int count;

    public synchronized void add() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

public class Thread_Demo4 {

    public static void main(String[] args) {
        Increast increast = new Increast();
        // 创建线程
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5_0000; i++) {
                increast.add();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5_0000; i++) {
                increast.add();
            }
        });

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("count :" + increast.getCount());
    }
}
